package com.yhty.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.yhty.dao.PayDao;
import com.yhty.model.Pay;
import com.yhty.model.PayMent;
import com.yhty.model.Rent;
import com.yhty.model.User;

/**
 * 缴费记录dao实现类
 * @author roy
 * 2011-3-27-下午05:55:31
 */
@Repository("payDao")
public class PayDaoImpl implements PayDao {
	private JdbcTemplate jdbcTemplate;

	@Resource(name = "jdbcTemplate")
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	/*
	 * 获取ID
	 */
	private Long getId(){
		String sql = "SELECT PAYMENT_SEQ_ID.NEXTVAL FROM DUAL";
		return jdbcTemplate.queryForLong(sql);
	}

	/* 添加缴费记录
	 * @see com.yhty.dao.PayDao#add(com.yhty.model.PayMent)
	 */
	public void add(PayMent payMent) {
		String sql_f = "insert into payment(id,rent_id," +
				"create_date,user_id,display_name";
		String sql_v = ")values("+getId()+","+payMent.getRent_id()
		+",'"+payMent.getCreate_date()+"',"+payMent.getUser().getUser_id()
		+",'"+payMent.getUser().getDisplayName()+"'";
		String sql_e = ")";
		if(payMent.getPay()!=null){
			sql_f += ",rent,manager,water,electric";
			sql_v += ","+ payMent.getPay().getRent() +
			","+ payMent.getPay().getManager() +
			","+ payMent.getPay().getWater() +
			","+ payMent.getPay().getElectric();
		}
		if(payMent.getPrepare() != null){
			sql_f += ",p_rent,p_manager";
			sql_v += ","+payMent.getPrepare().getRent()+
			","+ payMent.getPrepare().getManager();
		}
		if(payMent.getOwe() != null){
			sql_f += ",o_rent,o_manager,o_water,o_electric";
			sql_v += ","+payMent.getOwe().getRent()+
			","+ payMent.getOwe().getManager()+
			","+ payMent.getOwe().getWater()+
			","+ payMent.getOwe().getElectric();
		}
		jdbcTemplate.update(sql_f+sql_v+sql_e);
	}

	/*
	 * 根据出租信息获取本月缴费记录
	 * @see com.yhty.dao.PayDao#getByRent(com.yhty.model.Rent)
	 */
	public PayMent getByRent(Rent rent) throws EmptyResultDataAccessException{
		Calendar calendar = Calendar.getInstance();
		String sql = "select * from payment where rent_id="+rent.getId()
		+" and month(create_date)="+ (calendar.get(Calendar.MONTH)+1);
		return jdbcTemplate.queryForObject(sql, new RowMapper<PayMent>(){
			public PayMent mapRow(ResultSet rs, int numNo) throws SQLException {
				PayMent pm = new PayMent();
				pm.setId(rs.getLong("id"));
				pm.setCreate_date(getDate(rs.getTimestamp("create_date")));
				pm.setDelay(rs.getFloat("delay"));
				pm.setRent_id(rs.getLong("rent_id"));
				User u = new User();
				u.setUser_id(rs.getLong("user_id"));
				u.setDisplayName("display_name");
				pm.setUser(u);
				if(StringUtils.isNotBlank(rs.getString("rent"))){
					Pay pay = new Pay();
					pay.setRent(rs.getFloat("rent"));
					pay.setManager(rs.getFloat("manager"));
					pay.setWater(rs.getFloat("water"));
					pay.setElectric(rs.getFloat("electric"));
					pm.setPay(pay);
				}
				if(StringUtils.isNotBlank(rs.getString("o_rent"))){
					Pay owe = new Pay();
					owe.setRent(rs.getFloat("o_rent"));
					owe.setManager(rs.getFloat("o_manager"));
					owe.setWater(rs.getFloat("o_water"));
					owe.setElectric(rs.getFloat("o_electric"));
					pm.setOwe(owe);
				}
				if(StringUtils.isNotBlank(rs.getString("p_rent"))){
					Pay prepare = new Pay();
					prepare.setRent(rs.getFloat("p_rent"));
					prepare.setManager(rs.getFloat("p_manager"));
					pm.setPrepare(prepare);
				}
				return pm;
			}
		});
	}
	
	private String getDate(Timestamp ts) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = new Date(ts.getTime());
		return sdf.format(d);
	}

	/*
	 * 根据ID更新本月缴欠费
	 * @see com.yhty.dao.PayDao#updateOwe(com.yhty.model.PayMent)
	 */
	public void updateOwe(PayMent payMent) {
		String sql = "update payment set o_rent="
			+payMent.getOwe().getRent()
			+",o_manager="+payMent.getOwe().getManager()
			+",o_water="+payMent.getOwe().getWater()
			+",o_electric="+payMent.getOwe().getElectric()
			+" where id="+payMent.getId();
		jdbcTemplate.update(sql);
	}

	/*
	 * 根据ID更新本月缴费
	 * @see com.yhty.dao.PayDao#updatePay(com.yhty.model.PayMent)
	 */
	public void updatePay(PayMent payMent) {
		String sql = "update payment set id="+payMent.getId();
		if(payMent.getPay().getRent()!=null)
			sql += ",rent="+payMent.getPay().getRent()
			+",manager="+payMent.getPay().getManager();
		if(payMent.getPay().getWater()!=null)
			sql +=",water="+payMent.getPay().getWater()
			+",electric="+payMent.getPay().getElectric();
		sql +=" where id="+payMent.getId();
		jdbcTemplate.update(sql);
	}

	/*
	 * 根据ID更新本月预缴费
	 * @see com.yhty.dao.PayDao#updatePrepare(com.yhty.model.PayMent)
	 */
	public void updatePrepare(PayMent payMent) {
		String sql = "update payment set p_rent="
			+payMent.getPrepare().getRent()
			+",p_manager="+payMent.getPrepare().getManager()
			+" where id="+payMent.getId();
		jdbcTemplate.update(sql);
	}

}
